package com.hsingh.eclipse.mercury.extension.perspective;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;

import com.hsingh.eclipse.mercury.extensionpoint.definition.AbstractTask;
import com.hsingh.eclipse.mercury.extensionpoint.definition.ITask;

public class PerspectiveTask extends AbstractTask implements ITask {

  private final IPerspectiveDescriptor perspectiveDescriptor;

  public PerspectiveTask(IPerspectiveDescriptor perspectiveDescriptor) {
    this.perspectiveDescriptor = perspectiveDescriptor;
  }

  @Override
  public String getName() {
    return perspectiveDescriptor.getLabel();
  }

  @Override
  public String getLabel() {
    return getName();
  }

  @Override
  public String getDetailsLabel() {
    return perspectiveDescriptor.getDescription();
  }

  @Override
  public Object getItem() {
    return perspectiveDescriptor;
  }

  @Override
  public Image getImage() {
    return perspectiveDescriptor.getImageDescriptor().createImage();
  }

  @Override
  public void execute(ExecutionEvent event) {
    IWorkbench workbench = PlatformUI.getWorkbench();
    try {
      workbench.showPerspective(perspectiveDescriptor.getId(), workbench.getActiveWorkbenchWindow());
    } catch (WorkbenchException e) {
      e.printStackTrace();
    }
  }
}
